﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraNavBar;

namespace MM.Common.Setup
{
    public partial class FrmSetupTreeBase : MM.Commmon.UI.FrmFunctionWithTool
    {
        protected bool isEdit;
        protected bool isNew;
        public FrmSetupTreeBase()
        {
            this.InitializeComponent();
        }

        protected virtual void afterInsert()
        {
        }

        protected virtual bool beforeEdit()
        {
            return true;
        }

        protected virtual void beforeSaveRecord()
        {
        }

        protected virtual void changeMode(bool edit)
        {
            this.isEdit = edit;
            this.splitContainer1.Panel2.Enabled = this.isEdit;
            this.tvList.Enabled = !this.isEdit;
            this.nbiNew.Visible = !this.isEdit;
            this.nbiDelete.Visible = !this.isEdit;
            this.nbiEdit.Visible = !this.isEdit;
            this.nbiSave.Visible = this.isEdit;
            this.nbiCancel.Visible = this.isEdit;
        }

        protected virtual void deleteRecord()
        {
        }

        protected virtual void freshDisp()
        {
        }

        public override bool Init()
        {
            if (base.Init())
            {
                this.changeMode(false);
                this.tvList.AfterSelect += new TreeViewEventHandler(this.tvList_AfterSelect);
                return true;
            }
            return false;
        }

        private void nbiCancel_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.changeMode(false);
            this.freshDisp();
        }

        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.deleteRecord();
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.beforeEdit())
            {
                this.changeMode(true);
                this.isNew = false;
            }
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.changeMode(true);
            this.isNew = true;
            this.afterInsert();
        }

        private void nbiSave_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.beforeSaveRecord();
            if (this.saveRecord())
            {
                this.changeMode(false);
                this.freshDisp();
            }
        }

        protected virtual bool saveRecord()
        {
            return true;
        }

        private void tvList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.freshDisp();
        }
    }
}
